


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using SubSonic.DataProviders;
using SubSonic.Extensions;
using System.Linq.Expressions;
using SubSonic.Schema;
using System.Collections;
using SubSonic;
using SubSonic.Repository;
using System.ComponentModel;
using System.Data.Common;

namespace OrmSmackdown.SubSonic3
{
    
    
    /// <summary>
    /// A class which represents the Messages table in the ORM_Smackdown Database.
    /// </summary>
    public partial class Message: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<Message> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<Message>(new OrmSmackdown.SubSonic3.ORM_SmackdownDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<Message> testlist){
            SetTestRepo();
            _testRepo._items = testlist;
        }
        public static void Setup(Message item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                Message item=new Message();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<Message> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        OrmSmackdown.SubSonic3.ORM_SmackdownDB _db;
        public Message(string connectionString, string providerName) {

            _db=new OrmSmackdown.SubSonic3.ORM_SmackdownDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                Message.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<Message>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public Message(){
             _db=new OrmSmackdown.SubSonic3.ORM_SmackdownDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public Message(Expression<Func<Message, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<Message> GetRepo(string connectionString, string providerName){
            OrmSmackdown.SubSonic3.ORM_SmackdownDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new OrmSmackdown.SubSonic3.ORM_SmackdownDB();
            }else{
                db=new OrmSmackdown.SubSonic3.ORM_SmackdownDB(connectionString, providerName);
            }
            IRepository<Message> _repo;
            
            if(db.TestMode){
                Message.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<Message>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<Message> GetRepo(){
            return GetRepo("","");
        }
        
        public static Message SingleOrDefault(Expression<Func<Message, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            Message single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static Message SingleOrDefault(Expression<Func<Message, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            Message single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<Message, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<Message, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<Message> Find(Expression<Func<Message, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<Message> Find(Expression<Func<Message, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<Message> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<Message> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<Message> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<Message> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<Message> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<Message> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "MessageId";
        }

        public object KeyValue()
        {
            return this.MessageId;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
            return this.Subject.ToString();
        }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(Message)){
                Message compare=(Message)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        public override int GetHashCode() {
            return this.MessageId;
        }
        
        public string DescriptorValue()
        {
            return this.Subject.ToString();
        }

        public string DescriptorColumn() {
            return "Subject";
        }
        public static string GetKeyColumn()
        {
            return "MessageId";
        }        
        public static string GetDescriptorColumn()
        {
            return "Subject";
        }
        
        #region ' Foreign Keys '
        public IQueryable<Recipient> Recipients
        {
            get
            {
                
                  var repo=OrmSmackdown.SubSonic3.Recipient.GetRepo();
                  return from items in repo.GetAll()
                       where items.MessageId == _MessageId
                       select items;
            }
        }

        public IQueryable<Person> People
        {
            get
            {
                
                  var repo=OrmSmackdown.SubSonic3.Person.GetRepo();
                  return from items in repo.GetAll()
                       where items.PersonId == _SenderUserId
                       select items;
            }
        }

        #endregion
        

        int _MessageId;
        public int MessageId
        {
            get { return _MessageId; }
            set
            {
                if(_MessageId!=value){
                    _MessageId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="MessageId");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _SenderUserId;
        public int SenderUserId
        {
            get { return _SenderUserId; }
            set
            {
                if(_SenderUserId!=value){
                    _SenderUserId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="SenderUserId");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Subject;
        public string Subject
        {
            get { return _Subject; }
            set
            {
                if(_Subject!=value){
                    _Subject=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Subject");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Body;
        public string Body
        {
            get { return _Body; }
            set
            {
                if(_Body!=value){
                    _Body=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Body");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0)
                _repo.Update(this,provider);
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<Message, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the Addresses table in the ORM_Smackdown Database.
    /// </summary>
    public partial class Address: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<Address> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<Address>(new OrmSmackdown.SubSonic3.ORM_SmackdownDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<Address> testlist){
            SetTestRepo();
            _testRepo._items = testlist;
        }
        public static void Setup(Address item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                Address item=new Address();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<Address> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        OrmSmackdown.SubSonic3.ORM_SmackdownDB _db;
        public Address(string connectionString, string providerName) {

            _db=new OrmSmackdown.SubSonic3.ORM_SmackdownDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                Address.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<Address>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public Address(){
             _db=new OrmSmackdown.SubSonic3.ORM_SmackdownDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public Address(Expression<Func<Address, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<Address> GetRepo(string connectionString, string providerName){
            OrmSmackdown.SubSonic3.ORM_SmackdownDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new OrmSmackdown.SubSonic3.ORM_SmackdownDB();
            }else{
                db=new OrmSmackdown.SubSonic3.ORM_SmackdownDB(connectionString, providerName);
            }
            IRepository<Address> _repo;
            
            if(db.TestMode){
                Address.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<Address>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<Address> GetRepo(){
            return GetRepo("","");
        }
        
        public static Address SingleOrDefault(Expression<Func<Address, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            Address single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static Address SingleOrDefault(Expression<Func<Address, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            Address single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<Address, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<Address, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<Address> Find(Expression<Func<Address, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<Address> Find(Expression<Func<Address, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<Address> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<Address> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<Address> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<Address> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<Address> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<Address> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "AddressId";
        }

        public object KeyValue()
        {
            return this.AddressId;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
            return this.Line1.ToString();
        }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(Address)){
                Address compare=(Address)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        public override int GetHashCode() {
            return this.AddressId;
        }
        
        public string DescriptorValue()
        {
            return this.Line1.ToString();
        }

        public string DescriptorColumn() {
            return "Line1";
        }
        public static string GetKeyColumn()
        {
            return "AddressId";
        }        
        public static string GetDescriptorColumn()
        {
            return "Line1";
        }
        
        #region ' Foreign Keys '
        public IQueryable<Person> People
        {
            get
            {
                
                  var repo=OrmSmackdown.SubSonic3.Person.GetRepo();
                  return from items in repo.GetAll()
                       where items.PersonId == _PersonId
                       select items;
            }
        }

        #endregion
        

        int _AddressId;
        public int AddressId
        {
            get { return _AddressId; }
            set
            {
                if(_AddressId!=value){
                    _AddressId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="AddressId");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _PersonId;
        public int PersonId
        {
            get { return _PersonId; }
            set
            {
                if(_PersonId!=value){
                    _PersonId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="PersonId");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        bool _IsDefault;
        public bool IsDefault
        {
            get { return _IsDefault; }
            set
            {
                if(_IsDefault!=value){
                    _IsDefault=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="IsDefault");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Line1;
        public string Line1
        {
            get { return _Line1; }
            set
            {
                if(_Line1!=value){
                    _Line1=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Line1");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Line2;
        public string Line2
        {
            get { return _Line2; }
            set
            {
                if(_Line2!=value){
                    _Line2=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Line2");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _City;
        public string City
        {
            get { return _City; }
            set
            {
                if(_City!=value){
                    _City=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="City");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _State;
        public string State
        {
            get { return _State; }
            set
            {
                if(_State!=value){
                    _State=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="State");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _ZipCode;
        public string ZipCode
        {
            get { return _ZipCode; }
            set
            {
                if(_ZipCode!=value){
                    _ZipCode=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ZipCode");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _ZipCodePlusFour;
        public string ZipCodePlusFour
        {
            get { return _ZipCodePlusFour; }
            set
            {
                if(_ZipCodePlusFour!=value){
                    _ZipCodePlusFour=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ZipCodePlusFour");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0)
                _repo.Update(this,provider);
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<Address, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the Portals table in the ORM_Smackdown Database.
    /// </summary>
    public partial class Portal: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<Portal> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<Portal>(new OrmSmackdown.SubSonic3.ORM_SmackdownDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<Portal> testlist){
            SetTestRepo();
            _testRepo._items = testlist;
        }
        public static void Setup(Portal item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                Portal item=new Portal();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<Portal> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        OrmSmackdown.SubSonic3.ORM_SmackdownDB _db;
        public Portal(string connectionString, string providerName) {

            _db=new OrmSmackdown.SubSonic3.ORM_SmackdownDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                Portal.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<Portal>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public Portal(){
             _db=new OrmSmackdown.SubSonic3.ORM_SmackdownDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public Portal(Expression<Func<Portal, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<Portal> GetRepo(string connectionString, string providerName){
            OrmSmackdown.SubSonic3.ORM_SmackdownDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new OrmSmackdown.SubSonic3.ORM_SmackdownDB();
            }else{
                db=new OrmSmackdown.SubSonic3.ORM_SmackdownDB(connectionString, providerName);
            }
            IRepository<Portal> _repo;
            
            if(db.TestMode){
                Portal.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<Portal>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<Portal> GetRepo(){
            return GetRepo("","");
        }
        
        public static Portal SingleOrDefault(Expression<Func<Portal, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            Portal single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static Portal SingleOrDefault(Expression<Func<Portal, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            Portal single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<Portal, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<Portal, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<Portal> Find(Expression<Func<Portal, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<Portal> Find(Expression<Func<Portal, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<Portal> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<Portal> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<Portal> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<Portal> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<Portal> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<Portal> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "PortalId";
        }

        public object KeyValue()
        {
            return this.PortalId;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
            return this.PortalName.ToString();
        }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(Portal)){
                Portal compare=(Portal)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        public override int GetHashCode() {
            return this.PortalId;
        }
        
        public string DescriptorValue()
        {
            return this.PortalName.ToString();
        }

        public string DescriptorColumn() {
            return "PortalName";
        }
        public static string GetKeyColumn()
        {
            return "PortalId";
        }        
        public static string GetDescriptorColumn()
        {
            return "PortalName";
        }
        
        #region ' Foreign Keys '
        public IQueryable<PortalPeopleXref_L> PortalPeopleXref_Ls
        {
            get
            {
                
                  var repo=OrmSmackdown.SubSonic3.PortalPeopleXref_L.GetRepo();
                  return from items in repo.GetAll()
                       where items.PortalId == _PortalId
                       select items;
            }
        }

        public IQueryable<PortalPeopleXref> PortalPeopleXrefs
        {
            get
            {
                
                  var repo=OrmSmackdown.SubSonic3.PortalPeopleXref.GetRepo();
                  return from items in repo.GetAll()
                       where items.PortalId == _PortalId
                       select items;
            }
        }

        public IQueryable<Person> People
        {
            get
            {
                
                  var repo=OrmSmackdown.SubSonic3.Person.GetRepo();
                  return from items in repo.GetAll()
                       where items.PersonId == _AdminUserId
                       select items;
            }
        }

        #endregion
        

        int _PortalId;
        public int PortalId
        {
            get { return _PortalId; }
            set
            {
                if(_PortalId!=value){
                    _PortalId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="PortalId");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _PortalName;
        public string PortalName
        {
            get { return _PortalName; }
            set
            {
                if(_PortalName!=value){
                    _PortalName=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="PortalName");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _AdminUserId;
        public int AdminUserId
        {
            get { return _AdminUserId; }
            set
            {
                if(_AdminUserId!=value){
                    _AdminUserId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="AdminUserId");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0)
                _repo.Update(this,provider);
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<Portal, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the Recipients table in the ORM_Smackdown Database.
    /// </summary>
    public partial class Recipient: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<Recipient> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<Recipient>(new OrmSmackdown.SubSonic3.ORM_SmackdownDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<Recipient> testlist){
            SetTestRepo();
            _testRepo._items = testlist;
        }
        public static void Setup(Recipient item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                Recipient item=new Recipient();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<Recipient> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        OrmSmackdown.SubSonic3.ORM_SmackdownDB _db;
        public Recipient(string connectionString, string providerName) {

            _db=new OrmSmackdown.SubSonic3.ORM_SmackdownDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                Recipient.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<Recipient>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public Recipient(){
             _db=new OrmSmackdown.SubSonic3.ORM_SmackdownDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public Recipient(Expression<Func<Recipient, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<Recipient> GetRepo(string connectionString, string providerName){
            OrmSmackdown.SubSonic3.ORM_SmackdownDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new OrmSmackdown.SubSonic3.ORM_SmackdownDB();
            }else{
                db=new OrmSmackdown.SubSonic3.ORM_SmackdownDB(connectionString, providerName);
            }
            IRepository<Recipient> _repo;
            
            if(db.TestMode){
                Recipient.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<Recipient>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<Recipient> GetRepo(){
            return GetRepo("","");
        }
        
        public static Recipient SingleOrDefault(Expression<Func<Recipient, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            Recipient single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static Recipient SingleOrDefault(Expression<Func<Recipient, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            Recipient single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<Recipient, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<Recipient, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<Recipient> Find(Expression<Func<Recipient, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<Recipient> Find(Expression<Func<Recipient, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<Recipient> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<Recipient> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<Recipient> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<Recipient> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<Recipient> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<Recipient> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "RecipientId";
        }

        public object KeyValue()
        {
            return this.RecipientId;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
            return this.UserId.ToString();
        }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(Recipient)){
                Recipient compare=(Recipient)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        public override int GetHashCode() {
            return this.RecipientId;
        }
        
        public string DescriptorValue()
        {
            return this.UserId.ToString();
        }

        public string DescriptorColumn() {
            return "UserId";
        }
        public static string GetKeyColumn()
        {
            return "RecipientId";
        }        
        public static string GetDescriptorColumn()
        {
            return "UserId";
        }
        
        #region ' Foreign Keys '
        public IQueryable<Message> Messages
        {
            get
            {
                
                  var repo=OrmSmackdown.SubSonic3.Message.GetRepo();
                  return from items in repo.GetAll()
                       where items.MessageId == _MessageId
                       select items;
            }
        }

        public IQueryable<Person> People
        {
            get
            {
                
                  var repo=OrmSmackdown.SubSonic3.Person.GetRepo();
                  return from items in repo.GetAll()
                       where items.PersonId == _UserId
                       select items;
            }
        }

        #endregion
        

        int _RecipientId;
        public int RecipientId
        {
            get { return _RecipientId; }
            set
            {
                if(_RecipientId!=value){
                    _RecipientId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="RecipientId");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _UserId;
        public int UserId
        {
            get { return _UserId; }
            set
            {
                if(_UserId!=value){
                    _UserId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="UserId");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _MessageId;
        public int MessageId
        {
            get { return _MessageId; }
            set
            {
                if(_MessageId!=value){
                    _MessageId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="MessageId");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0)
                _repo.Update(this,provider);
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<Recipient, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the PortalPeopleXref table in the ORM_Smackdown Database.
    /// </summary>
    public partial class PortalPeopleXref: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<PortalPeopleXref> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<PortalPeopleXref>(new OrmSmackdown.SubSonic3.ORM_SmackdownDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<PortalPeopleXref> testlist){
            SetTestRepo();
            _testRepo._items = testlist;
        }
        public static void Setup(PortalPeopleXref item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                PortalPeopleXref item=new PortalPeopleXref();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<PortalPeopleXref> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        OrmSmackdown.SubSonic3.ORM_SmackdownDB _db;
        public PortalPeopleXref(string connectionString, string providerName) {

            _db=new OrmSmackdown.SubSonic3.ORM_SmackdownDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                PortalPeopleXref.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<PortalPeopleXref>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public PortalPeopleXref(){
             _db=new OrmSmackdown.SubSonic3.ORM_SmackdownDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public PortalPeopleXref(Expression<Func<PortalPeopleXref, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<PortalPeopleXref> GetRepo(string connectionString, string providerName){
            OrmSmackdown.SubSonic3.ORM_SmackdownDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new OrmSmackdown.SubSonic3.ORM_SmackdownDB();
            }else{
                db=new OrmSmackdown.SubSonic3.ORM_SmackdownDB(connectionString, providerName);
            }
            IRepository<PortalPeopleXref> _repo;
            
            if(db.TestMode){
                PortalPeopleXref.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<PortalPeopleXref>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<PortalPeopleXref> GetRepo(){
            return GetRepo("","");
        }
        
        public static PortalPeopleXref SingleOrDefault(Expression<Func<PortalPeopleXref, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            PortalPeopleXref single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static PortalPeopleXref SingleOrDefault(Expression<Func<PortalPeopleXref, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            PortalPeopleXref single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<PortalPeopleXref, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<PortalPeopleXref, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<PortalPeopleXref> Find(Expression<Func<PortalPeopleXref, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<PortalPeopleXref> Find(Expression<Func<PortalPeopleXref, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<PortalPeopleXref> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<PortalPeopleXref> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<PortalPeopleXref> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<PortalPeopleXref> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<PortalPeopleXref> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<PortalPeopleXref> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "PersonId";
        }

        public object KeyValue()
        {
            return this.PersonId;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
            return this.PersonId.ToString();
        }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(PortalPeopleXref)){
                PortalPeopleXref compare=(PortalPeopleXref)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        public override int GetHashCode() {
            return this.PersonId;
        }
        
        public string DescriptorValue()
        {
            return this.PersonId.ToString();
        }

        public string DescriptorColumn() {
            return "PersonId";
        }
        public static string GetKeyColumn()
        {
            return "PersonId";
        }        
        public static string GetDescriptorColumn()
        {
            return "PersonId";
        }
        
        #region ' Foreign Keys '
        public IQueryable<Portal> Portals
        {
            get
            {
                
                  var repo=OrmSmackdown.SubSonic3.Portal.GetRepo();
                  return from items in repo.GetAll()
                       where items.PortalId == _PortalId
                       select items;
            }
        }

        public IQueryable<Person> People
        {
            get
            {
                
                  var repo=OrmSmackdown.SubSonic3.Person.GetRepo();
                  return from items in repo.GetAll()
                       where items.PersonId == _PersonId
                       select items;
            }
        }

        #endregion
        

        int _PortalId;
        public int PortalId
        {
            get { return _PortalId; }
            set
            {
                if(_PortalId!=value){
                    _PortalId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="PortalId");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _PersonId;
        public int PersonId
        {
            get { return _PersonId; }
            set
            {
                if(_PersonId!=value){
                    _PersonId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="PersonId");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0)
                _repo.Update(this,provider);
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<PortalPeopleXref, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the PortalPeopleXref_LS table in the ORM_Smackdown Database.
    /// </summary>
    public partial class PortalPeopleXref_L: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<PortalPeopleXref_L> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<PortalPeopleXref_L>(new OrmSmackdown.SubSonic3.ORM_SmackdownDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<PortalPeopleXref_L> testlist){
            SetTestRepo();
            _testRepo._items = testlist;
        }
        public static void Setup(PortalPeopleXref_L item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                PortalPeopleXref_L item=new PortalPeopleXref_L();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<PortalPeopleXref_L> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        OrmSmackdown.SubSonic3.ORM_SmackdownDB _db;
        public PortalPeopleXref_L(string connectionString, string providerName) {

            _db=new OrmSmackdown.SubSonic3.ORM_SmackdownDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                PortalPeopleXref_L.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<PortalPeopleXref_L>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public PortalPeopleXref_L(){
             _db=new OrmSmackdown.SubSonic3.ORM_SmackdownDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public PortalPeopleXref_L(Expression<Func<PortalPeopleXref_L, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<PortalPeopleXref_L> GetRepo(string connectionString, string providerName){
            OrmSmackdown.SubSonic3.ORM_SmackdownDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new OrmSmackdown.SubSonic3.ORM_SmackdownDB();
            }else{
                db=new OrmSmackdown.SubSonic3.ORM_SmackdownDB(connectionString, providerName);
            }
            IRepository<PortalPeopleXref_L> _repo;
            
            if(db.TestMode){
                PortalPeopleXref_L.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<PortalPeopleXref_L>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<PortalPeopleXref_L> GetRepo(){
            return GetRepo("","");
        }
        
        public static PortalPeopleXref_L SingleOrDefault(Expression<Func<PortalPeopleXref_L, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            PortalPeopleXref_L single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static PortalPeopleXref_L SingleOrDefault(Expression<Func<PortalPeopleXref_L, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            PortalPeopleXref_L single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<PortalPeopleXref_L, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<PortalPeopleXref_L, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<PortalPeopleXref_L> Find(Expression<Func<PortalPeopleXref_L, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<PortalPeopleXref_L> Find(Expression<Func<PortalPeopleXref_L, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<PortalPeopleXref_L> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<PortalPeopleXref_L> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<PortalPeopleXref_L> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<PortalPeopleXref_L> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<PortalPeopleXref_L> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<PortalPeopleXref_L> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "PortalPeopleXrefId";
        }

        public object KeyValue()
        {
            return this.PortalPeopleXrefId;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
            return this.PortalId.ToString();
        }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(PortalPeopleXref_L)){
                PortalPeopleXref_L compare=(PortalPeopleXref_L)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        public override int GetHashCode() {
            return this.PortalPeopleXrefId;
        }
        
        public string DescriptorValue()
        {
            return this.PortalId.ToString();
        }

        public string DescriptorColumn() {
            return "PortalId";
        }
        public static string GetKeyColumn()
        {
            return "PortalPeopleXrefId";
        }        
        public static string GetDescriptorColumn()
        {
            return "PortalId";
        }
        
        #region ' Foreign Keys '
        public IQueryable<Portal> Portals
        {
            get
            {
                
                  var repo=OrmSmackdown.SubSonic3.Portal.GetRepo();
                  return from items in repo.GetAll()
                       where items.PortalId == _PortalId
                       select items;
            }
        }

        public IQueryable<Person> People
        {
            get
            {
                
                  var repo=OrmSmackdown.SubSonic3.Person.GetRepo();
                  return from items in repo.GetAll()
                       where items.PersonId == _PersonId
                       select items;
            }
        }

        #endregion
        

        int _PortalPeopleXrefId;
        public int PortalPeopleXrefId
        {
            get { return _PortalPeopleXrefId; }
            set
            {
                if(_PortalPeopleXrefId!=value){
                    _PortalPeopleXrefId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="PortalPeopleXrefId");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _PortalId;
        public int PortalId
        {
            get { return _PortalId; }
            set
            {
                if(_PortalId!=value){
                    _PortalId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="PortalId");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _PersonId;
        public int PersonId
        {
            get { return _PersonId; }
            set
            {
                if(_PersonId!=value){
                    _PersonId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="PersonId");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0)
                _repo.Update(this,provider);
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<PortalPeopleXref_L, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the People table in the ORM_Smackdown Database.
    /// </summary>
    public partial class Person: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<Person> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<Person>(new OrmSmackdown.SubSonic3.ORM_SmackdownDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<Person> testlist){
            SetTestRepo();
            _testRepo._items = testlist;
        }
        public static void Setup(Person item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                Person item=new Person();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<Person> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        OrmSmackdown.SubSonic3.ORM_SmackdownDB _db;
        public Person(string connectionString, string providerName) {

            _db=new OrmSmackdown.SubSonic3.ORM_SmackdownDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                Person.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<Person>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public Person(){
             _db=new OrmSmackdown.SubSonic3.ORM_SmackdownDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public Person(Expression<Func<Person, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<Person> GetRepo(string connectionString, string providerName){
            OrmSmackdown.SubSonic3.ORM_SmackdownDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new OrmSmackdown.SubSonic3.ORM_SmackdownDB();
            }else{
                db=new OrmSmackdown.SubSonic3.ORM_SmackdownDB(connectionString, providerName);
            }
            IRepository<Person> _repo;
            
            if(db.TestMode){
                Person.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<Person>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<Person> GetRepo(){
            return GetRepo("","");
        }
        
        public static Person SingleOrDefault(Expression<Func<Person, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            Person single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static Person SingleOrDefault(Expression<Func<Person, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            Person single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<Person, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<Person, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<Person> Find(Expression<Func<Person, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<Person> Find(Expression<Func<Person, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<Person> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<Person> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<Person> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<Person> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<Person> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<Person> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "PersonId";
        }

        public object KeyValue()
        {
            return this.PersonId;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
            return this.FirstName.ToString();
        }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(Person)){
                Person compare=(Person)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        public override int GetHashCode() {
            return this.PersonId;
        }
        
        public string DescriptorValue()
        {
            return this.FirstName.ToString();
        }

        public string DescriptorColumn() {
            return "FirstName";
        }
        public static string GetKeyColumn()
        {
            return "PersonId";
        }        
        public static string GetDescriptorColumn()
        {
            return "FirstName";
        }
        
        #region ' Foreign Keys '
        public IQueryable<Company> Companies
        {
            get
            {
                
                  var repo=OrmSmackdown.SubSonic3.Company.GetRepo();
                  return from items in repo.GetAll()
                       where items.CompanyId == _CompanyId
                       select items;
            }
        }

        public IQueryable<Role> Roles
        {
            get
            {
                
                  var repo=OrmSmackdown.SubSonic3.Role.GetRepo();
                  return from items in repo.GetAll()
                       where items.RoleId == _RoleId
                       select items;
            }
        }

        public IQueryable<Address> Addresses
        {
            get
            {
                
                  var repo=OrmSmackdown.SubSonic3.Address.GetRepo();
                  return from items in repo.GetAll()
                       where items.PersonId == _PersonId
                       select items;
            }
        }

        public IQueryable<Message> Messages
        {
            get
            {
                
                  var repo=OrmSmackdown.SubSonic3.Message.GetRepo();
                  return from items in repo.GetAll()
                       where items.SenderUserId == _PersonId
                       select items;
            }
        }

        public IQueryable<Person> People
        {
            get
            {
                
                  var repo=OrmSmackdown.SubSonic3.Person.GetRepo();
                  return from items in repo.GetAll()
                       where items.PersonId == _ManagerPersonId
                       select items;
            }
        }

        public IQueryable<PortalPeopleXref_L> PortalPeopleXref_Ls
        {
            get
            {
                
                  var repo=OrmSmackdown.SubSonic3.PortalPeopleXref_L.GetRepo();
                  return from items in repo.GetAll()
                       where items.PersonId == _PersonId
                       select items;
            }
        }

        public IQueryable<PortalPeopleXref> PortalPeopleXrefs
        {
            get
            {
                
                  var repo=OrmSmackdown.SubSonic3.PortalPeopleXref.GetRepo();
                  return from items in repo.GetAll()
                       where items.PersonId == _PersonId
                       select items;
            }
        }

        public IQueryable<Portal> Portals
        {
            get
            {
                
                  var repo=OrmSmackdown.SubSonic3.Portal.GetRepo();
                  return from items in repo.GetAll()
                       where items.AdminUserId == _PersonId
                       select items;
            }
        }

        public IQueryable<Recipient> Recipients
        {
            get
            {
                
                  var repo=OrmSmackdown.SubSonic3.Recipient.GetRepo();
                  return from items in repo.GetAll()
                       where items.UserId == _PersonId
                       select items;
            }
        }

        #endregion
        

        int _PersonId;
        public int PersonId
        {
            get { return _PersonId; }
            set
            {
                if(_PersonId!=value){
                    _PersonId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="PersonId");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _FirstName;
        public string FirstName
        {
            get { return _FirstName; }
            set
            {
                if(_FirstName!=value){
                    _FirstName=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="FirstName");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _LastName;
        public string LastName
        {
            get { return _LastName; }
            set
            {
                if(_LastName!=value){
                    _LastName=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="LastName");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Email;
        public string Email
        {
            get { return _Email; }
            set
            {
                if(_Email!=value){
                    _Email=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Email");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _CompanyId;
        public int? CompanyId
        {
            get { return _CompanyId; }
            set
            {
                if(_CompanyId!=value){
                    _CompanyId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="CompanyId");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _RoleId;
        public int? RoleId
        {
            get { return _RoleId; }
            set
            {
                if(_RoleId!=value){
                    _RoleId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="RoleId");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _ManagerPersonId;
        public int? ManagerPersonId
        {
            get { return _ManagerPersonId; }
            set
            {
                if(_ManagerPersonId!=value){
                    _ManagerPersonId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ManagerPersonId");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _UserType;
        public string UserType
        {
            get { return _UserType; }
            set
            {
                if(_UserType!=value){
                    _UserType=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="UserType");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0)
                _repo.Update(this,provider);
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<Person, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the Performance table in the ORM_Smackdown Database.
    /// </summary>
    public partial class Performance: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<Performance> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<Performance>(new OrmSmackdown.SubSonic3.ORM_SmackdownDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<Performance> testlist){
            SetTestRepo();
            _testRepo._items = testlist;
        }
        public static void Setup(Performance item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                Performance item=new Performance();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<Performance> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        OrmSmackdown.SubSonic3.ORM_SmackdownDB _db;
        public Performance(string connectionString, string providerName) {

            _db=new OrmSmackdown.SubSonic3.ORM_SmackdownDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                Performance.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<Performance>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public Performance(){
             _db=new OrmSmackdown.SubSonic3.ORM_SmackdownDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public Performance(Expression<Func<Performance, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<Performance> GetRepo(string connectionString, string providerName){
            OrmSmackdown.SubSonic3.ORM_SmackdownDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new OrmSmackdown.SubSonic3.ORM_SmackdownDB();
            }else{
                db=new OrmSmackdown.SubSonic3.ORM_SmackdownDB(connectionString, providerName);
            }
            IRepository<Performance> _repo;
            
            if(db.TestMode){
                Performance.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<Performance>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<Performance> GetRepo(){
            return GetRepo("","");
        }
        
        public static Performance SingleOrDefault(Expression<Func<Performance, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            Performance single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static Performance SingleOrDefault(Expression<Func<Performance, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            Performance single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<Performance, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<Performance, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<Performance> Find(Expression<Func<Performance, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<Performance> Find(Expression<Func<Performance, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<Performance> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<Performance> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<Performance> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<Performance> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<Performance> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<Performance> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "PerformanceId";
        }

        public object KeyValue()
        {
            return this.PerformanceId;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
            return this.OrmName.ToString();
        }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(Performance)){
                Performance compare=(Performance)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        public override int GetHashCode() {
            return this.PerformanceId;
        }
        
        public string DescriptorValue()
        {
            return this.OrmName.ToString();
        }

        public string DescriptorColumn() {
            return "OrmName";
        }
        public static string GetKeyColumn()
        {
            return "PerformanceId";
        }        
        public static string GetDescriptorColumn()
        {
            return "OrmName";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _PerformanceId;
        public int PerformanceId
        {
            get { return _PerformanceId; }
            set
            {
                if(_PerformanceId!=value){
                    _PerformanceId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="PerformanceId");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _OrmName;
        public string OrmName
        {
            get { return _OrmName; }
            set
            {
                if(_OrmName!=value){
                    _OrmName=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="OrmName");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Action;
        public string Action
        {
            get { return _Action; }
            set
            {
                if(_Action!=value){
                    _Action=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Action");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _MilliSeconds;
        public int MilliSeconds
        {
            get { return _MilliSeconds; }
            set
            {
                if(_MilliSeconds!=value){
                    _MilliSeconds=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="MilliSeconds");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0)
                _repo.Update(this,provider);
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<Performance, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the Roles table in the ORM_Smackdown Database.
    /// </summary>
    public partial class Role: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<Role> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<Role>(new OrmSmackdown.SubSonic3.ORM_SmackdownDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<Role> testlist){
            SetTestRepo();
            _testRepo._items = testlist;
        }
        public static void Setup(Role item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                Role item=new Role();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<Role> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        OrmSmackdown.SubSonic3.ORM_SmackdownDB _db;
        public Role(string connectionString, string providerName) {

            _db=new OrmSmackdown.SubSonic3.ORM_SmackdownDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                Role.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<Role>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public Role(){
             _db=new OrmSmackdown.SubSonic3.ORM_SmackdownDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public Role(Expression<Func<Role, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<Role> GetRepo(string connectionString, string providerName){
            OrmSmackdown.SubSonic3.ORM_SmackdownDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new OrmSmackdown.SubSonic3.ORM_SmackdownDB();
            }else{
                db=new OrmSmackdown.SubSonic3.ORM_SmackdownDB(connectionString, providerName);
            }
            IRepository<Role> _repo;
            
            if(db.TestMode){
                Role.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<Role>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<Role> GetRepo(){
            return GetRepo("","");
        }
        
        public static Role SingleOrDefault(Expression<Func<Role, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            Role single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static Role SingleOrDefault(Expression<Func<Role, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            Role single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<Role, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<Role, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<Role> Find(Expression<Func<Role, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<Role> Find(Expression<Func<Role, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<Role> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<Role> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<Role> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<Role> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<Role> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<Role> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "RoleId";
        }

        public object KeyValue()
        {
            return this.RoleId;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
            return this.RoleName.ToString();
        }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(Role)){
                Role compare=(Role)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        public override int GetHashCode() {
            return this.RoleId;
        }
        
        public string DescriptorValue()
        {
            return this.RoleName.ToString();
        }

        public string DescriptorColumn() {
            return "RoleName";
        }
        public static string GetKeyColumn()
        {
            return "RoleId";
        }        
        public static string GetDescriptorColumn()
        {
            return "RoleName";
        }
        
        #region ' Foreign Keys '
        public IQueryable<Person> People
        {
            get
            {
                
                  var repo=OrmSmackdown.SubSonic3.Person.GetRepo();
                  return from items in repo.GetAll()
                       where items.RoleId == _RoleId
                       select items;
            }
        }

        #endregion
        

        int _RoleId;
        public int RoleId
        {
            get { return _RoleId; }
            set
            {
                if(_RoleId!=value){
                    _RoleId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="RoleId");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _RoleName;
        public string RoleName
        {
            get { return _RoleName; }
            set
            {
                if(_RoleName!=value){
                    _RoleName=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="RoleName");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        bool _IsForPortal;
        public bool IsForPortal
        {
            get { return _IsForPortal; }
            set
            {
                if(_IsForPortal!=value){
                    _IsForPortal=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="IsForPortal");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        bool _IsForContact;
        public bool IsForContact
        {
            get { return _IsForContact; }
            set
            {
                if(_IsForContact!=value){
                    _IsForContact=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="IsForContact");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0)
                _repo.Update(this,provider);
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<Role, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the Companies table in the ORM_Smackdown Database.
    /// </summary>
    public partial class Company: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<Company> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<Company>(new OrmSmackdown.SubSonic3.ORM_SmackdownDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<Company> testlist){
            SetTestRepo();
            _testRepo._items = testlist;
        }
        public static void Setup(Company item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                Company item=new Company();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<Company> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        OrmSmackdown.SubSonic3.ORM_SmackdownDB _db;
        public Company(string connectionString, string providerName) {

            _db=new OrmSmackdown.SubSonic3.ORM_SmackdownDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                Company.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<Company>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public Company(){
             _db=new OrmSmackdown.SubSonic3.ORM_SmackdownDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public Company(Expression<Func<Company, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<Company> GetRepo(string connectionString, string providerName){
            OrmSmackdown.SubSonic3.ORM_SmackdownDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new OrmSmackdown.SubSonic3.ORM_SmackdownDB();
            }else{
                db=new OrmSmackdown.SubSonic3.ORM_SmackdownDB(connectionString, providerName);
            }
            IRepository<Company> _repo;
            
            if(db.TestMode){
                Company.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<Company>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<Company> GetRepo(){
            return GetRepo("","");
        }
        
        public static Company SingleOrDefault(Expression<Func<Company, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            Company single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static Company SingleOrDefault(Expression<Func<Company, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            Company single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<Company, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<Company, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<Company> Find(Expression<Func<Company, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<Company> Find(Expression<Func<Company, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<Company> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<Company> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<Company> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<Company> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<Company> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<Company> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "CompanyId";
        }

        public object KeyValue()
        {
            return this.CompanyId;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
            return this.CompanyName.ToString();
        }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(Company)){
                Company compare=(Company)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        public override int GetHashCode() {
            return this.CompanyId;
        }
        
        public string DescriptorValue()
        {
            return this.CompanyName.ToString();
        }

        public string DescriptorColumn() {
            return "CompanyName";
        }
        public static string GetKeyColumn()
        {
            return "CompanyId";
        }        
        public static string GetDescriptorColumn()
        {
            return "CompanyName";
        }
        
        #region ' Foreign Keys '
        public IQueryable<Person> People
        {
            get
            {
                
                  var repo=OrmSmackdown.SubSonic3.Person.GetRepo();
                  return from items in repo.GetAll()
                       where items.CompanyId == _CompanyId
                       select items;
            }
        }

        #endregion
        

        int _CompanyId;
        public int CompanyId
        {
            get { return _CompanyId; }
            set
            {
                if(_CompanyId!=value){
                    _CompanyId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="CompanyId");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _CompanyName;
        public string CompanyName
        {
            get { return _CompanyName; }
            set
            {
                if(_CompanyName!=value){
                    _CompanyName=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="CompanyName");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0)
                _repo.Update(this,provider);
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<Company, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
}
